package cuit.game.controller;



import java.io.File;
import java.io.IOException;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


import cuit.game.entity.Picture;

import cuit.game.entity.User;

import cuit.game.service.PictureService;
import cuit.game.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;


@Controller
@RequestMapping("/picture")
public class PictureController {
	@Autowired
	PictureService pictureService;
	@Autowired
	UserService userService;
	@Value("${reggie.path}")
    private String basePath;
	@Value("${niumo.path}")
    private String choubing;
	
	
	@PostMapping("/upload")
    public String upload(MultipartFile file,HttpServletRequest request) throws IOException {
		User user;
		User ruser = null;
		HttpSession session = request.getSession();
		user = (User) session.getAttribute("user");
		ruser = userService.findById(user.getId());
        String originalFilename = file.getOriginalFilename();
        String fileName=originalFilename.substring(originalFilename.lastIndexOf("."));
        String NewfileName= UUID.randomUUID().toString()+fileName;
        //判断要存放的目录是否存在，不存在则创建
        File dir=new File(basePath);
        if (!dir.exists()){
            dir.mkdirs();
        }
        Picture picture = new Picture();
        picture.setFilename(NewfileName);
        picture.setUser(ruser);
        picture.setUrl(choubing+NewfileName);
        pictureService.save(picture);
        file.transferTo(new File(basePath+NewfileName));

            return "redirect:/picture/list";
    }

	//寻找该用户的证书
	@GetMapping("/list")
	public String findAllById(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		User user= (User) session.getAttribute("user");
		List<Picture> picturelist;
		if(user.getId()!=5) {
			 picturelist = pictureService.findByUser_id(user.getId());
		}
		else {
			 picturelist = pictureService.findAll();
		}
		
		model.addAttribute("pictureList", picturelist);
		return "picture/list";
	}
	//更新状态
	@GetMapping("/update")
	public String update(@RequestParam("Id") long Id, HttpServletRequest request, Model model) {
		
		HttpSession session = request.getSession();
		User user= (User) session.getAttribute("user");
		
		Picture pic = pictureService.findById(Id);
		pic.setStatu(1);
		List<Picture> picturelist = pictureService.findAll();
		model.addAttribute("pictureList", picturelist);
		return "picture/list";
	}
}